package com.climb.ssm.util;

import java.io.IOException;
import java.util.Properties;

/**
 * @author Administrator
 * @date 2021-06-03 17:49:44
 * @description
 */
public class BeanFactory2 {
    /**文件加载工具*/
    public static Properties properties;
    static {
        properties=new Properties();
        /*获取propertie文件流到流对象并加载内容到properties*/
        try {
            properties.load(BeanFactory2.class.getClassLoader().getResourceAsStream("bean.properties"));
        } catch (IOException e) {
            throw new ExceptionInInitializerError("初始化properties失败！");
        }

    }
    /*每次都会创建一个新的对象*/
    public  static Object getBean(String beanName){
        Object bean = null;
        try {
            String beanClassName = properties.getProperty(beanName);
            //每次都会调用默认构造函数创建对象
            bean = Class.forName(beanClassName).getDeclaredConstructor().newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
        return bean;
    }

/*无法对更改这个bean name*/
/*    static Object bean = null;
    public synchronized static Object getBean(String beanName){
        if(bean!=null){return bean;}

            if (bean==null){
                try {
                    String beanClassName = properties.getProperty(beanName);
                    //每次都会调用默认构造函数创建对象
                    bean = Class.forName(beanClassName).getDeclaredConstructor().newInstance();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }



        return bean;
    }*/
}
